/*=============================================================================

  SYSTEM MANAGER CAMERA INTERFACE

  FILE: SYSmgr_HCam.cpp
  
  DESCRIPTION

    CAM Interface Module Definition

  EXTERNALIZED FUNCTIONS

    
  Copyright (c) DaeSund Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

                             EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when           who       what, where, why
  ---------  -------  -------------------------------------------------------
   13/10/18     jyko       Initial Version

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include "SYSmgr.h"

/*=============================================================================
  CONSTANT DEFINITION
=============================================================================*/

/*=============================================================================
  LOCALIZED FUNCTION DEFINITION
=============================================================================*/
static dword HCamEventCallback(dword dwEvent, dword wParam, dword lParam, mcxt pUser);

/*=============================================================================
  CLASS IMPLEMENTATION
=============================================================================*/
void SYSmgr::HCam_Alloc(void)
{
	ObjClient_s		Cli;

	m_pCam = NULL;

	m_pCam = new HCAM;
	m_camInit = FALSE;
	m_camCFG = NULL;

	Cli.pUserCxt = this;
	Cli.EventCbf = HCamEventCallback;
	m_pCam->SetClient(&Cli);
}

void SYSmgr::HCam_Free(void)
{
	if(m_pCam == NULL)
		return;

	m_pCam->Close();

	if(m_camCFG)
	{
		memFree(m_camCFG);
		m_camCFG = NULL;
	}	
	delete m_pCam;
	m_pCam = NULL;
}

boolean SYSmgr::HCam_Setup(char *pszFile)
{
	if(m_camCFG)
	{
		memFree(m_camCFG);
		m_camCFG = NULL;
	}

	if(pszFile != NULL)
	{
		m_camCFG = (char*)memCalloc(strlen(pszFile) + 1);
		strcpy(m_camCFG, pszFile);
	}

	return	TRUE;
}

boolean SYSmgr::HCam_Init(void)
{
	cam_cfg_s		*cfg;

	Event_Handler(HCAM_DEV, INIT_START_E, 0, 0);

	if(m_camInit == TRUE)
	{
		Event_Handler(HCAM_DEV, INIT_DONE_E, 0, 0);
		return	TRUE;
	}

	// Check the configuration file
	cfg = cam_cfg_load(m_camCFG);
	if(cfg == NULL)
	{
		cfg = cam_default_alloc();
		if(cam_cfg_save(cfg, m_camCFG) == FALSE)
		{
			cam_default_free(cfg);

			return	FALSE;
		}
		cam_default_free(cfg);
	}
	else
	{
		cam_cfg_free(cfg);
	}

	// Initialize the device
	if(m_pCam->Init(m_camCFG))
	{
		m_camInit = TRUE;
		Event_Handler(HCAM_DEV, INIT_DONE_E, 0, 0);
		return	TRUE;
	}

	Event_Handler(HCAM_DEV, ERR_INIT_E, 0, 0);

	return	FALSE;
}

boolean SYSmgr::HCam_Close(void)
{
	Event_Handler(HCAM_DEV, CLOSE_START_E, 0, 0);

	if(!m_camInit)
	{
		Event_Handler(HCAM_DEV, CLOSE_DONE_E, 0, 0);
		return	TRUE;
	}

	if(m_pCam->Close() == TRUE)
	{
		m_camInit = FALSE;
		Event_Handler(HCAM_DEV, CLOSE_DONE_E, 0, 0);
		return	TRUE;
	}

	m_camInit = FALSE;
	Event_Handler(HCAM_DEV, ERR_CLOSE_E, 0, 0);

	return	FALSE;
}

/*=============================================================================
  LOCALIZED FUNCTION IMPLEMENTATION
=============================================================================*/
dword HCamEventCallback(dword dwEvent, dword wParam, dword lParam, mcxt pUser)
{
	SYSmgr		*pCxt = (SYSmgr*)pUser;

	if(pCxt)
	{
		return	pCxt->HEvent_Handler(HCAM_DEV, dwEvent, wParam, lParam);
	}
	return	(0);
}

/*--- END OF SYSmgr_HCam.CPP ---*/
